<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/25
 * Time: 14:49
 */
class GameController extends CommonController
{
  private $model;
  public function __construct()
  {
      parent::__construct();
      $this->model = D('Game');
  }

  /**
  * 游戏列表
  * @author wang
  * @param 
  */
  public function gameList(){
    $where = array();
    $sort = I('request.sort', 'orderid');
    $order = I('request.order', 'asc');
    $search = I('search');
    if(!empty($search)) $where['g.game|g.description|a.app|t.type'] = array('like','%'.$search.'%');
    $result = M('game as g')
             ->join('LEFT JOIN '.C('DB_PREFIX').'app as a ON g.appid = a.appid')
             ->join('LEFT JOIN '.C('DB_PREFIX').'type as t ON g.typecode = t.typeid')
             ->field('g.*,a.app,t.type')
             ->where($where)
             ->order($sort." ".$order)
             ->select(); 
    foreach ($result as $k => &$v) {
      $v['createtime']=date("Y-m-d H:i:s",$v['createtime']);
      $v['updatetime']=date("Y-m-d H:i:s",$v['updatetime']);
      $v['orderid']=(int)$v['orderid'];
    }
    $this->ajaxReturn($result);
  }


  /**
  * 查看游戏详情
  * @author wang
  * @param 
  */
  public function gameDetial(){
    $gameid = I('gameid');
    $data = M('game as g')
            ->join('LEFT JOIN '.C('DB_PREFIX').'app as a ON g.appid = a.appid')
            ->join('LEFT JOIN '.C('DB_PREFIX').'type as t ON g.typecode = t.typeid')
            ->join('LEFT JOIN '.C('DB_PREFIX').'area as d ON g.areacode = d.areacode')
            ->field('g.*,a.app,t.type,d.area')
            ->where("g.gameid='$gameid'")
            ->find();
    $diqu2 = str_split($data['areacode'],2);
    if( $diqu2['0']!=71 && $diqu2['0']!=81 && $diqu2['0']!=82 ){
        $sheng = M('area')->field('area')->where("areacode =".$diqu2['0']."0000")->find();
        $shi = M('area')->field('area')->where("areacode =".$diqu2['0'].$diqu2['1']."00")->find();
    }
    $data['createtime']=date("Y-m-d H:i:s",$data['createtime']);
    $data['updatetime']=date("Y-m-d H:i:s",$data['updatetime']);
    $listObj = M('Area');
    $where['areacode'] = array('like','%0000');
    $listprovince = $listObj->where($where)->select();
    $type_id = M('Type')->select();
    $app_id = M('App')->select();
    $flag = M('Cascadedata')->where('datagroup="flag"')->select();
    $datap = M('game')->field('picarr,icon,poster,adpic')->where("gameid='$gameid'")->find();
    $datap['picarr'] = unserialize($datap['picarr']);
    $this->assign("datap",$datap);
    $this->assign("data",$data);
    $this->assign("province_list",$listprovince);
    $this->assign("type_id",$type_id);
    $this->assign("app_id",$app_id);
    $this->assign("flag",$flag);
    $this->assign("sheng",$sheng);
    $this->assign("shi",$shi);
    $this->display('detial');
  }

  /**
  * 修改内容/新增游戏
  * @author wang
  * @param 
  */
  public function addImg(){
    $data = I();
    $img = $this->uploadImg();
    foreach ($img as $key => $val) {
      foreach ($val as $k => $v) {
        $data[$key][] = $v; 
      }
    }
    $data['picarr'] = xulie($data['picarr']) ? xulie($data['picarr']) : '';
    $data['icon'] = implode(" ",$data['icon']);
    $data['poster'] = implode(" ",$data['poster']);
    $data['adpic'] = implode(" ",$data['adpic']);
    $data['admin'] = session('admin');
    $data['updatetime'] = time();
    $gameid = $data['gameid'];
    $data['flag'] = implode(",",$data['flag']);

    // print_r($data);
    // exit;
    if($gameid){
        if(M('game')->where("gameid='$gameid'")->save($data)){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }else{
        $data['admin'] = session('admin');
        $data['createtime'] = time();
        // $data['id'] = (new  \Org\Twitter\IdWork(31,31))->nextId();
        if(M('game')->add($data)){
            $this->ajaxReturn(2);
        }else{
            $this->ajaxReturn(0);
        }
    }


  }


  /**
  * 删除游戏
  * @author wang
  * @param 
  */
  public function delGame(){
    $gameid = I('gameid');
      if($this->model->where("gameid='$gameid'")->delete()){
          M('currency')-> where("gameid='$gameid'")->setField('gameid','');
          M('item')-> where("gameid='$gameid'")->setField('gameid','');
          M('mode')-> where("gameid='$gameid'")->setField('gameid','');
        $result = array(
          'sta' => 1, 
          'msg' => '删除成功'
        );
      }else{
        $result = array(
          'sta' => 0, 
          'msg' => '删除失败'
        ); 
      }
    $this->ajaxReturn($result);
  }

  /**
  * 省、类型、app
  * @author wang
  * @param 
  */
  public function index(){
    $listObj = M('Area');
    $where['areacode'] = array('like','%0000');
    $listprovince = $listObj->where($where)->select();
    $type_id = M('Type')->select();
    $app_id = M('App')->select();
    $flag = M('Cascadedata')->where('datagroup="flag"')->select();
    $this->assign("province_list",$listprovince);
    $this->assign("flag",$flag);
    $this->assign("type_id",$type_id);
    $this->assign("app_id",$app_id);
    $this->display();
  }

  /**
  * 市
  * @author wang
  * @param 
  */
  public function shi(){
    $listObj = M('Area');
    $sheng = I('sheng');
    $shilist = $listObj->where("areacode like '".$sheng."%' AND areacode like '%00' AND areacode not like '%0000'")->select();
    $data=array('status'=>0,'city'=>$shilist);
    $this->ajaxReturn($data);
  }

  /**
  * 县区
  * @author wang
  * @param 
  */
  public function xian(){
    $listObj = M('Area');
    $xian = I('xian');
    $xianlist = $listObj->where("areacode like '".$xian."%' AND areacode not like '%00'")->select();
    $data=array('status'=>0,'district'=>$xianlist);
    $this->ajaxReturn($data);
  }


}